{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b3a6ae7-59e0-4498-814e-a93e1d634cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aa8584e9-8f08-4e67-bea5-4a8c6fb7ac94",
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.util.PriorityQueue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ab39a624-cec1-4102-b23d-640c312b72dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "575b8a31-6e1c-4edc-98f8-72c52992797b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd799fac-9c82-4c5a-b308-60691a01a866",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get().trim()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cac8a111-9146-49ea-8989-c6392869eaaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a064468c-9c51-4307-ba19-e3ff83c6c4ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"1163751742\n",
    "1381373672\n",
    "2136511328\n",
    "3694931569\n",
    "7463417111\n",
    "1319128137\n",
    "1359912421\n",
    "3125421639\n",
    "1293138521\n",
    "2311944581\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "878489a4-5e1e-4c04-a209-650185a8de67",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c3e422a6-51fa-4631-82dd-0ef856817384",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun solve(a: List<List<Int>>): Int {\n",
    "    val n = a.size\n",
    "    val m = a[0].size\n",
    "    val d = Array(n) { IntArray(m) { Int.MAX_VALUE } }\n",
    "    d[0][0] = 0\n",
    "    val pq = PriorityQueue<Triple<Int, Int, Int>>(compareBy {it.first} )\n",
    "    pq.add(Triple(0, 0, 0))\n",
    "    \n",
    "    while (pq.isNotEmpty()) {\n",
    "        val (dis, i, j) = pq.poll()\n",
    "        if (dis > d[i][j])\n",
    "            continue\n",
    "        \n",
    "        for ((di, dj) in listOf(Pair(-1, 0), Pair(0, -1), Pair(1, 0), Pair(0, 1))) {\n",
    "            val ni = i + di\n",
    "            val nj = j + dj\n",
    "            if (ni >= 0 && ni < n && nj >= 0 && nj < m && dis + a[ni][nj] < d[ni][nj]) {\n",
    "                d[ni][nj] = dis + a[ni][nj]\n",
    "                pq.add(Triple(d[ni][nj], ni, nj))\n",
    "            }\n",
    "        }\n",
    "        \n",
    "    }\n",
    "    \n",
    "    return d[n - 1][m - 1]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "07df67e5-7a24-42e8-a6c4-f555675939df",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    val lines = input.split('\\n')\n",
    "    val a = lines.map { it.toList().map { it.code - '0'.code } }\n",
    "    return solve(a).toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eaa273a8-a166-44e0-9023-2449fecbbb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2ad99e07-5a96-40c4-873c-e72dd7c5f499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "441"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23679ce7-8258-41ae-9813-dff5745b482c",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "528387cf-abb5-4da2-9fd3-9bbf865444a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String): String {\n",
    "    val lines = input.split('\\n')\n",
    "    val ca = lines.map { it.toList().map { it.code - '0'.code } }\n",
    "    val n = ca.size\n",
    "    val m = ca[0].size\n",
    "    val a = mutableListOf<MutableList<Int>>()\n",
    "    for (i in 0 until 5) \n",
    "        for (j in 0 until 5)\n",
    "            for (p in 0 until n) {\n",
    "                if (j == 0)\n",
    "                    a.add(mutableListOf<Int>())\n",
    "                for (q in 0 until m)\n",
    "                    a[i * n + p].add((ca[p][q] + i + j - 1) % 9 + 1)\n",
    "            }\n",
    "    \n",
    "    return solve(a).toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b12a86b5-8d6f-4a6c-851a-af1d84ae168d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "315"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "62a5e94e-550f-42fe-9597-32e6f87a5b1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2849"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb4614cf-b32f-4f3b-93ce-298387832db7",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-5432"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
